﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Shop;
using System.Data;
using System.Data.SqlClient;

namespace YXShop.SQLServerDAL.Shop
{
    public class ShopColor:IShopColor
    {

        #region DataBase Operation

        public int Add(YXShop.Model.Shop.ShopColor model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_shopcolor([name],[depict],[samplemappath],[isstate],[version],[crateTime],[templatepath])");
            strSql.Append(" values(");
            strSql.Append("@name,@depict,@samplemappath,@isstate,@version,@crateTime,@templatepath ) Select scope_IDENTITY() ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParas(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(),parameters);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }

        public int Update(YXShop.Model.Shop.ShopColor model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [yxs_shopcolor] set ");
            strSql.Append(" name=@name,depict=@depict,samplemappath=@samplemappath,isstate=@isstate,version=@version,crateTime=@crateTime,templatepath=@templatepath ");
            strSql.Append(" where  id=@id");
            SqlParameter[] parameters=(SqlParameter[])this.ValueParas(model);
            return ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_shopcolor] ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters ={
                                           new SqlParameter("@id",SqlDbType.Int,4)
                                      };
            parameters[0].Value = id;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameters);
        }

        #endregion

        #region DataBase Load

        public ChangeHope.DataBase.DataByPage GetList(string where)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_shopcolor [where] 1=1 ");
            if(where!=null&&where!="")
            {
                strSql.Append(where);
            }
            strSql.Append(" [order by] id ASC ");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        public YXShop.Model.Shop.ShopColor GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_shopcolor ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters ={
                                           new SqlParameter("@id",SqlDbType.Int,4)
                                      };
            parameters[0].Value = id;
            YXShop.Model.Shop.ShopColor model = null;
            using(SqlDataReader reader=ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(),parameters))
            {
                if(reader.Read())
                {
                    model = new YXShop.Model.Shop.ShopColor();
                    model.Id = (int)reader["id"];
                    model.Name = (string)reader["name"];
                    model.Samplemappath = (string)reader["samplemappath"];
                    model.Depict = (string)reader["depict"];
                    model.Isstate = (int)reader["isstate"];
                    model.CrateTime = Convert.ToDateTime(reader["crateTime"]);
                    model.Templatepath = (string)reader["templatepath"];
                    model.Version = (string)reader["version"];
                }
            }
            return model;
        }
        public List<YXShop.Model.Shop.ShopColor> GetALL(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_shopcolor ");
            if(where!=null&&where!="")
            {
                strSql.Append(" where "+where+" ");
            }
            List<YXShop.Model.Shop.ShopColor> list = new List<YXShop.Model.Shop.ShopColor>();
            using(SqlDataReader reader=ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while(reader.Read())
                {
                    YXShop.Model.Shop.ShopColor model = new YXShop.Model.Shop.ShopColor();
                    model.Id = (int)reader["id"];
                    model.Name = (string)reader["name"];
                    model.Samplemappath = (string)reader["samplemappath"];
                    model.Depict = (string)reader["depict"];
                    model.Isstate = (int)reader["isstate"];
                    model.CrateTime = Convert.ToDateTime(reader["crateTime"]);
                    model.Templatepath = (string)reader["templatepath"];
                    model.Version = (string)reader["version"];
                    list.Add(model);
                }
            }
            return list;
        }
        #endregion

        #region Other Operation
        public IDbDataParameter[] ValueParas(YXShop.Model.Shop.ShopColor model)
        {
            SqlParameter[] parameters ={   
                                           new SqlParameter("@id",SqlDbType.Int,4),
                                           new SqlParameter("@name",SqlDbType.VarChar,50),
                                           new SqlParameter("@depict",SqlDbType.VarChar,500),
                                           new SqlParameter("@samplemappath",SqlDbType.VarChar,100),
                                           new SqlParameter("@isstate",SqlDbType.Int,4),
                                           new SqlParameter("@version",SqlDbType.VarChar,200),
                                           new SqlParameter("@crateTime",SqlDbType.DateTime,8),
                                           new SqlParameter("@templatepath",SqlDbType.VarChar,50)

                                      };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Depict;
            parameters[3].Value = model.Samplemappath;
            parameters[4].Value = model.Isstate;
            parameters[5].Value = model.Version;
            parameters[6].Value = model.CrateTime;
            parameters[7].Value = model.Templatepath;
            return parameters;
        }
        #endregion
    }
}
